package com.learn.springboot.service.utils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.function.Function;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.learn.springboot.dao.mode.PageParameter;
import com.learn.springboot.dao.request.CommonPageResult;
import com.learn.springboot.dao.request.PageQueryRequest;

import cn.hutool.core.collection.CollectionUtil;


public class PageUtils {

    public static Page toPage(PageQueryRequest pageRequest) {
        return new Page(pageRequest.getCurrentPage(), pageRequest.getPageSize());
    }


    public static Page toPage(PageParameter pageRequest) {
        return new Page(pageRequest.getCurrentPage(), pageRequest.getPageSize());
    }

    public static <T> Page<T> toPage(PageQueryRequest pageRequest, Function<Page<T>, List<T>> function) {
        Page<T> page = toPage(pageRequest);
        List<T> records = function.apply(page);
        page.setRecords(records);
        return page;
    }



    public static <T> CommonPageResult<T> toPageResult(PageQueryRequest pagerRequest, Function<Page<T>, List<T>> function) {
        Page<T> page = toPage(pagerRequest, function);
        int totalCount = (int) page.getTotal();
        int totalPage = ConverterUtil.calculateTotalPage((int) page.getSize(), totalCount);
        return ConverterUtil.convertToPageResult(page.getRecords(), totalCount, totalPage);
    }



    public static <T, U> CommonPageResult<U> toPageResult(Page<T> page, Function<Page<T>, List<U>> getRecordsFunc) {
        int totalCount = (int) page.getTotal();
        int totalPage = ConverterUtil.calculateTotalPage((int) page.getSize(), totalCount);
        List<U> data;
        if (page.getRecords() == null || page.getRecords().size() <= 0) {
            data = Collections.emptyList();
        } else {
            data = getRecordsFunc.apply(page);
        }
        return ConverterUtil.convertToPageResult(data, totalCount, totalPage);
    }

    public static <T> CommonPageResult<T> toPageResult(Page<T> page, List<T> dataList) {
        int totalCount = (int) page.getTotal();
        int totalPage = ConverterUtil.calculateTotalPage((int) page.getSize(), totalCount);
        List<T> data;
        if (CollectionUtil.isEmpty(dataList)) {
            data = Collections.emptyList();
        } else {
            data = dataList;
        }
        return ConverterUtil.convertToPageResult(data, totalCount, totalPage);
    }

    public static <T, U> CommonPageResult<U> toPageResult(CommonPageResult<T> originCommonPageResult, Function<T, U> converter) {

        CommonPageResult<U> newCommonPageResult = new CommonPageResult<>();

        newCommonPageResult.setData(
                Optional.ofNullable(originCommonPageResult.getData()).map(data ->
                        data.stream().map(converter).collect(Collectors.toList())
                ).orElse(null)
        );
        newCommonPageResult.setTotalPage(originCommonPageResult.getTotalPage());
        newCommonPageResult.setTotalCount(originCommonPageResult.getTotalCount());
        return newCommonPageResult;

    }


}
